package cn.itcast.web.controller.cargo;

import cn.itcast.dao.cargo.ContractDao;
import cn.itcast.domain.vo.ContractProductVo;
import cn.itcast.service.cargo.ContractService;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletOutputStream;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/cargo/contract")
public class OutProductController extends BaseController {
    @Reference
    private ContractService contractService;
    /**
     * 请求路径/cargo/contract/print.do
     * 响应路径："cargo/print/contract-print";
     */
    //点击进入页面
    @RequestMapping("print")
    public String print(){
        return "cargo/print/contract-print";
    }

    /**
     *  请求路径：/cargo/contract/printExcel.do
     *  请求参数：inputDate
     *  响应路径："cargo/print/contract-print";
     */
    @RequestMapping("printExcel")
    public void  printExcel(String inputDate) throws IOException {
        //一.导出第一行
        //创建工作薄（07版本的）
        Workbook workbook = new SXSSFWorkbook();
        //2.创建工作表
        Sheet sheet = workbook.createSheet("出货表");
        //3.设置单元格宽度，参数2的单位，1/256
        sheet.setColumnWidth(0,256*5);
        sheet.setColumnWidth(1,256*26);
        sheet.setColumnWidth(2,256*11);
        sheet.setColumnWidth(3,256*29);
        sheet.setColumnWidth(4,256*12);
        sheet.setColumnWidth(5,256*15);
        sheet.setColumnWidth(6,256*10);
        sheet.setColumnWidth(7,256*10);
        sheet.setColumnWidth(8,256*10);

        //合并单元格（开始0行，结束0行，开始列1，结束8）
        sheet.addMergedRegion(new CellRangeAddress(0,0,1,8));

        //二：创建第二行，导出数据
        Row row = sheet.createRow(1);
        //样式
        row.setHeightInPoints(26);

        //定义一个数组：写入第二行的数据
        String[] titles={"客户","订单号","货号","数量","工厂","工厂交期","船期","贸易条款"};
        for (int i = 0; i < titles.length; i++) {
            //创建第二行的每一列
            Cell cell = row.createCell(i + 1);
            //设置内容
            cell.setCellValue(titles[i]);
            //设置样式
            cell.setCellStyle(this.title(workbook));
        }

        //三：导出数据行（调用service查询）
          //参数1：根据日期查询，dao映射语句：and c.ship_time Like'2019-07%'
        List<ContractProductVo> list = contractService.findByShipTime(inputDate + "%", getLoginCompanyId());
        if (list!=null && list.size()>0){
            int index=2;
            for (ContractProductVo cp : list) {
                for (int i = 0; i < 30000; i++) {


                //创建第三行从第三行开始
                row = sheet.createRow(index++);


                Cell cell =null;

                cell = row.createCell(1);
                cell.setCellValue(cp.getCustomName());
               // cell.setCellStyle(this.title(workbook));

                cell = row.createCell(2);
                cell.setCellValue(cp.getContractNo());
                //cell.setCellStyle(this.title(workbook));

                cell = row.createCell(3);
                cell.setCellValue(cp.getProductNo());
                //cell.setCellStyle(this.title(workbook));

                cell = row.createCell(4);
                cell.setCellValue(cp.getCnumber());
               // cell.setCellStyle(this.title(workbook));

                cell = row.createCell(5);
                cell.setCellValue(cp.getFactoryName());
               // cell.setCellStyle(this.title(workbook));

                cell = row.createCell(6);
                cell.setCellValue(cp.getDeliveryPeriod());
                //cell.setCellStyle(this.title(workbook));

                cell = row.createCell(7);
                cell.setCellValue(cp.getShipTime());
               // cell.setCellStyle(this.title(workbook));

                cell = row.createCell(8);
                cell.setCellValue(cp.getTradeTerms());
                //cell.setCellStyle(this.title(workbook));
                    }
            }
        }
        //四：导出下载
        //1.设置响应头（http协议的固定下载格式）
        response.setHeader("content-disposition","attachment;fileName=export.xlsx");
        //获取response输出流
        ServletOutputStream out = response.getOutputStream();
        //把excel文件写入到response输出流，实现下载
        workbook.write(out);
        //关流
        workbook.close();
        out.close();
    }




    //大标题的样式
    public CellStyle bigTitle(Workbook wb){
        CellStyle style = wb.createCellStyle();
        Font font = wb.createFont();
        font.setFontName("宋体");
        font.setFontHeightInPoints((short)16);
        font.setBold(true);//字体加粗
        style.setFont(font);
        style.setAlignment(HorizontalAlignment.CENTER);				//横向居中
        style.setVerticalAlignment(VerticalAlignment.CENTER);		//纵向居中
        return style;
    }

    //小标题的样式
    public CellStyle title(Workbook wb){
        CellStyle style = wb.createCellStyle();
        Font font = wb.createFont();
        font.setFontName("黑体");
        font.setFontHeightInPoints((short)12);
        style.setFont(font);
        style.setAlignment(HorizontalAlignment.CENTER);				//横向居中
        style.setVerticalAlignment(VerticalAlignment.CENTER);		//纵向居中
        style.setBorderTop(BorderStyle.THIN);						//上细线
        style.setBorderBottom(BorderStyle.THIN);					//下细线
        style.setBorderLeft(BorderStyle.THIN);						//左细线
        style.setBorderRight(BorderStyle.THIN);						//右细线
        return style;
    }

    //文字样式
    public CellStyle text(Workbook wb){
        CellStyle style = wb.createCellStyle();
        Font font = wb.createFont();
        font.setFontName("Times New Roman");
        font.setFontHeightInPoints((short)10);

        style.setFont(font);

        style.setAlignment(HorizontalAlignment.LEFT);				//横向居左
        style.setVerticalAlignment(VerticalAlignment.CENTER);		//纵向居中
        style.setBorderTop(BorderStyle.THIN);						//上细线
        style.setBorderBottom(BorderStyle.THIN);					//下细线
        style.setBorderLeft(BorderStyle.THIN);						//左细线
        style.setBorderRight(BorderStyle.THIN);						//右细线

        return style;
    }
}
